using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserVipCard
	{
		public bool Exists(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserVipCard");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public long Add(HKB.Data.Model.UserVipCard model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserVipCard(");
			stringBuilder.Append("CardNumber,CardCategory,FaceValue,PurchaseUserID,PurchaseDate,Balance,CouponAmount)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@CardNumber,@CardCategory,@FaceValue,@PurchaseUserID,@PurchaseDate,@Balance,@CouponAmount)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 10),
				new SqlParameter("@CardCategory", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@PurchaseUserID", SqlDbType.Int, 4),
				new SqlParameter("@PurchaseDate", SqlDbType.Date, 3),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@CouponAmount", SqlDbType.Decimal, 9)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.CardCategory;
			array[2].Value = model.FaceValue;
			array[3].Value = model.PurchaseUserID;
			array[4].Value = model.PurchaseDate;
			array[5].Value = model.Balance;
			array[6].Value = model.CouponAmount;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			long result;
			if (single == null)
			{
				result = 0L;
			}
			else
			{
				result = System.Convert.ToInt64(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.UserVipCard model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserVipCard set ");
			stringBuilder.Append("CardNumber=@CardNumber,");
			stringBuilder.Append("CardCategory=@CardCategory,");
			stringBuilder.Append("FaceValue=@FaceValue,");
			stringBuilder.Append("PurchaseUserID=@PurchaseUserID,");
			stringBuilder.Append("PurchaseDate=@PurchaseDate,");
			stringBuilder.Append("Balance=@Balance,");
			stringBuilder.Append("CouponAmount=@CouponAmount");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 10),
				new SqlParameter("@CardCategory", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@PurchaseUserID", SqlDbType.Int, 4),
				new SqlParameter("@PurchaseDate", SqlDbType.Date, 3),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@CouponAmount", SqlDbType.Decimal, 9),
				new SqlParameter("@id", SqlDbType.BigInt, 8)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.CardCategory;
			array[2].Value = model.FaceValue;
			array[3].Value = model.PurchaseUserID;
			array[4].Value = model.PurchaseDate;
			array[5].Value = model.Balance;
			array[6].Value = model.CouponAmount;
			array[7].Value = model.id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserVipCard ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string idlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserVipCard ");
			stringBuilder.Append(" where id in (" + idlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.UserVipCard GetModel(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 id,CardNumber,CardCategory,FaceValue,PurchaseUserID,PurchaseDate,Balance,CouponAmount from MBF_UserVipCard ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			HKB.Data.Model.UserVipCard userVipCard = new HKB.Data.Model.UserVipCard();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.UserVipCard result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.UserVipCard DataRowToModel(DataRow row)
		{
			HKB.Data.Model.UserVipCard userVipCard = new HKB.Data.Model.UserVipCard();
			if (row != null)
			{
				if (row["id"] != null && row["id"].ToString() != "")
				{
					userVipCard.id = long.Parse(row["id"].ToString());
				}
				if (row["CardNumber"] != null)
				{
					userVipCard.CardNumber = row["CardNumber"].ToString();
				}
				if (row["CardCategory"] != null)
				{
					userVipCard.CardCategory = row["CardCategory"].ToString();
				}
				if (row["FaceValue"] != null && row["FaceValue"].ToString() != "")
				{
					userVipCard.FaceValue = new decimal?(decimal.Parse(row["FaceValue"].ToString()));
				}
				if (row["PurchaseUserID"] != null && row["PurchaseUserID"].ToString() != "")
				{
					userVipCard.PurchaseUserID = new int?(int.Parse(row["PurchaseUserID"].ToString()));
				}
				if (row["PurchaseDate"] != null && row["PurchaseDate"].ToString() != "")
				{
					userVipCard.PurchaseDate = new System.DateTime?(System.DateTime.Parse(row["PurchaseDate"].ToString()));
				}
				if (row["Balance"] != null && row["Balance"].ToString() != "")
				{
					userVipCard.Balance = new decimal?(decimal.Parse(row["Balance"].ToString()));
				}
				if (row["CouponAmount"] != null && row["CouponAmount"].ToString() != "")
				{
					userVipCard.CouponAmount = new decimal?(decimal.Parse(row["CouponAmount"].ToString()));
				}
			}
			return userVipCard;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select id,CardNumber,CardCategory,FaceValue,PurchaseUserID,PurchaseDate,Balance,CouponAmount ");
			stringBuilder.Append(" FROM MBF_UserVipCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" id,CardNumber,CardCategory,FaceValue,PurchaseUserID,PurchaseDate,Balance,CouponAmount ");
			stringBuilder.Append(" FROM MBF_UserVipCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM MBF_UserVipCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.id desc");
			}
			stringBuilder.Append(")AS Row, T.*  from MBF_UserVipCard T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
